package com.quectel.aiot.controller;

import com.quectel.aiot.entity.Device;
import com.quectel.aiot.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/devices")
@Api(tags = "设备管理", description = "设备管理相关接口")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @PostMapping("/sync/product/{productKey}")
    @ApiOperation("同步指定产品下的设备")
    public String syncDevices(
            @ApiParam(name = "productKey", value = "产品Key", required = true) 
            @PathVariable String productKey) {
        try {
            deviceService.syncDevices(productKey);
            return "设备同步成功";
        } catch (Exception e) {
            log.error("设备同步失败", e);
            return "设备同步失败: " + e.getMessage();
        }
    }

    @PostMapping("/sync/all")
    @ApiOperation("同步所有产品的设备")
    public String syncAllProductsDevices() {
        try {
            deviceService.syncAllProductsDevices();
            return "所有产品设备同步成功";
        } catch (Exception e) {
            log.error("所有产品设备同步失败", e);
            return "所有产品设备同步失败: " + e.getMessage();
        }
    }

    @PostMapping("/sync/product/{productKey}/device/{deviceKey}")
    @ApiOperation("同步指定产品下的指定设备")
    public String syncDevice(
            @ApiParam(name = "productKey", value = "产品Key", required = true) @PathVariable String productKey,
            @ApiParam(name = "deviceKey", value = "设备Key", required = true) @PathVariable String deviceKey) {
        try {
            deviceService.syncDevice(productKey, deviceKey);
            return "设备同步成功";
        } catch (Exception e) {
            log.error("设备同步失败", e);
            return "设备同步失败: " + e.getMessage();
        }
    }

    @GetMapping
    @ApiOperation("获取所有设备列表")
    public List<Device> getAllDevices() {
        return deviceService.getAllDevices();
    }

    @GetMapping("/{deviceId}")
    @ApiOperation("根据设备ID获取设备")
    public Device getDeviceById(
            @ApiParam(name = "deviceId", value = "设备ID", required = true)
            @PathVariable String deviceId) {
        return deviceService.getDeviceById(deviceId);
    }

    @GetMapping("/product/{productKey}")
    @ApiOperation("获取指定产品下的所有设备")
    public List<Device> getDevicesByProductKey(
            @ApiParam(name = "productKey", value = "产品Key", required = true)
            @PathVariable String productKey) {
        return deviceService.getDevicesByProductKey(productKey);
    }

    @DeleteMapping("/{deviceId}")
    @ApiOperation("删除设备")
    public String deleteDevice(
            @ApiParam(name = "deviceId", value = "设备ID", required = true)
            @PathVariable String deviceId) {
        try {
            deviceService.deleteDevice(deviceId);
            return "设备删除成功";
        } catch (Exception e) {
            log.error("设备删除失败", e);
            return "设备删除失败: " + e.getMessage();
        }
    }
} 